package com.macro.mall.tiny.modules.foster.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.foster.model.PetStatus;
import com.macro.mall.tiny.modules.foster.model.PetStatusPicture;
import com.macro.mall.tiny.modules.foster.model.dto.PetStatusDTO;
import com.macro.mall.tiny.modules.foster.service.PetStatusPictureService;
import com.macro.mall.tiny.modules.foster.service.PetStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuyi
 * @since 2024-03-30
 */
@Api(tags = "PetStatusController", description = "宠物状态管理")
@RestController
@RequestMapping("/foster/petStatus")
@CrossOrigin
public class PetStatusController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PetStatusController.class);
    @Autowired
    private PetStatusService petStatusService;
    @Autowired
    private PetStatusPictureService petStatusPictureService;
    /**
     * 添加宠物状态
     *
     * @param
     * @return
     */
    @ApiOperation(value = "添加宠物状态")
    @RequestMapping(value = "/addPetStatus", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult addPetStatus(@RequestBody PetStatusDTO petStatusDTO) {
        return petStatusService.savePetStatus(petStatusDTO);
    }

    /**
     * 删除宠物状态
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除宠物状态")
    @RequestMapping(value = "/detelePetStatusById/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult deletePetStatus(@PathVariable Long id) {
        PetStatus status = petStatusService.getById(id);
        petStatusService.removeById(id);
        //删除相应的图片信息
        QueryWrapper<PetStatusPicture> wrapper = new QueryWrapper<>();
        wrapper
                .eq("pet_id",status.getPetId());
        petStatusPictureService.remove(wrapper);
        LOGGER.info("宠物状态删除成功");
        return CommonResult.success("宠物状态删除成功");
    }

    /**
     * 修改宠物状态
     *
     * @param
     * @return
     */
    @ApiOperation(value = "修改宠物状态")
    @RequestMapping(value = "/updatePetStatus", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('admin')")
    public CommonResult updatePetStatus(@RequestBody PetStatusDTO petStatusDTO) {
        return petStatusService.updateStatus(petStatusDTO);
    }

//    /**
//     * 分页查询宠物状态
//     *
//     * @param pageNum
//     * @param pageSize
//     * @return
//     */
//    @ApiOperation("分页查询宠物状态")
//    @PreAuthorize("hasAnyAuthority('user','admin')")
//    @RequestMapping(value = "/list", method = RequestMethod.GET)
//    public CommonResult<CommonPage<PetStatus>> list(@RequestParam(value = "pageNum", defaultValue = "1")
//                                                       @ApiParam("页码") Integer pageNum,
//                                                       @RequestParam(value = "pageSize", defaultValue = "5")
//                                                       @ApiParam("每页数量") Integer pageSize) {
//        Page<PetStatus> page = new Page<>(pageNum, pageSize);
//        Page<PetStatus> pageResult = petStatusService.page(page);
//        LOGGER.info("宠物状态查询状态：查询成功");
//        return CommonResult.success(CommonPage.restPage(pageResult));
//    }


    /**
     * 获取指定宠物id的宠物状态
     * @param id
     * @return
     */
    @ApiOperation("获取指定宠物id的宠物状态")
    @RequestMapping(value = "/getPetStatusById/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('user','admin')")
    public CommonResult getPetStatusById(@PathVariable("id") Long id) {
        LOGGER.info("指定id的宠物状态查询状态：查询成功");
        return petStatusService.selectByPetId(id);
    }
}
